package gameEngine.com.Actor;

import java.util.Random;

import android.graphics.Canvas;

import gameEngine.com.GameLayer;
import gameEngine.com.GameLib;
import gameEngine.com.IConstant;
import gameEngine.com.MainActivity;
import gameEngine.com.Sprite;
import gameEngine.com.state.StateGameplay;

public class Bullet extends Actor implements IConstant {
	public String levelString = " ";
	public static Sprite publicSprite;
	public int directMove_x = 0;
	public int directMove_y = 0;
	public int angleDegree = 0;
	public double angleRadian = 0;
	// public static Sprite commonSprite ;

	public static final int STATE_MOVE = 0;
	public static final int STATE_TRACK_MC_MOVE = 1;
	public static final int STATE_ATTACK_MC = 2;
	public static final int STATE_EXPLOSION = 3;
	public static final int STATE_RELEASE = 4;
	public static int BULLET_SPEED = 16;
	public static final int BULLET_NORMAL_WIDHT = 18;
	public static final int BULLET_NORMAL_HEIGHT = 18;
	public boolean attackMC = false;
	public boolean attackEnemy = false;

	public Bullet() {
		// TODO Auto-generated constructor stub
		super();
	}

	public static void init() {

	}

	public Bullet(int x, int y, int w, int h, String name, String type) {
		// TODO Auto-generated constructor stub
		super(x, y, w, h, name, type);
		state = STATE_MOVE;
	}

	public Bullet(int m_angle, int x, int y, int w, int h, String name, String type) {
		// TODO Auto-generated constructor stub
		super(x, y, w, h, name, type);
		angleDegree = m_angle;
		state = STATE_MOVE;
	}

	public void render() {

		// drawAnim_(Canvas g, int angle,int Scalex,int Scaley,int _currentAnimation,int _currentFrame, int _waitDelay, int x, int y)
		// drawAnim(MainActivity.mainCanvas,0,1,1, _currentAnimation,_currentFrame,_waitDelay,m_x + GameLayer.screenOffsetX, m_y + GameLayer.screenOffsetY);
		publicSprite.drawAnim(MainActivity.mainCanvas, angleDegree - 90, 1, 1, this, m_x + GameLayer.screenOffsetX, m_y + GameLayer.screenOffsetY);
		// Log.d("   " , " "+( m_x + GameLayer.screenOffsetX));
		// GameLib.mainCanvas.drawRect((m_x + GameLayer.screenOffsetX), (m_y + GameLayer.screenOffsetY), (m_x + GameLayer.screenOffsetX) + m_Width, (m_y + GameLayer.screenOffsetY) + m_Height, GameLib.mainPaint);
		// MainActivity.fontsmall.drawString(GameLib.mainCanvas, " " +angleDegree, (m_x + GameLayer.screenOffsetX) -10, (m_y + GameLayer.screenOffsetY)-15, 0);
		// GameLayer.screenOffsetY)-15, 0);
		drawRect(m_x+ GameLayer.screenOffsetX,m_y+ GameLayer.screenOffsetY,m_Width ,m_Height );
	}

	public void updateMove() {
		for (int i = 0; i < StateGameplay.listTankEmeny.size(); i++) {
			if (attackEnemy) {
				boolean checkCollion = Actor.checkCollision(this, StateGameplay.listTankEmeny.get(i));
				if (checkCollion) {
					StateGameplay.listTankEmeny.get(i).heath--;
					if (StateGameplay.listTankEmeny.get(i).heath > 0)
					{
						state = STATE_EXPLOSION;
						publicSprite.setAnim(this, 1, false, false);
					} else
					{
						StateGameplay.listTankEmeny.get(i).publicSprite.setAnim(StateGameplay.listTankEmeny.get(i), 1, false, false);
						state = -1;
						StateGameplay.listTankEmeny.get(i).state = TankEnemy.STATE_DIE;
					}
				}
			}
		}
		if (attackMC) {
			boolean checkCollion = Actor.checkCollision(this,
					StateGameplay.tankMC);
			if (checkCollion) {
				StateGameplay.tankMC.heath--;
				if (StateGameplay.tankMC.heath <= 0) {
					StateGameplay.tankMC.sprite.setAnim(1, false, false);
					state = -1;
					StateGameplay.tankMC.state = TankMC.STATE_DIE;
				}
				else {
					state = STATE_EXPLOSION;
					publicSprite.setAnim(this, 1, false, false);
				}
			}
		}
		if (!GameLayer.checkCollisionPhysic(GameLayer.arrayListDataMap
				.get(GameLayer.arrayListDataMap.size() - 1),
				m_x + directMove_x, m_y + directMove_y, 32, 32) 
				&& state != STATE_EXPLOSION) {
			double_m_x -= BULLET_SPEED * Math.cos(angleRadian);
			double_m_y -= BULLET_SPEED * Math.sin(angleRadian);
			m_x = (int) double_m_x;
			m_y = (int) double_m_y;

		} else // set new target
		{
			state = STATE_EXPLOSION;
			publicSprite.setAnim(this, 1, false, false);

		}

	}

	public void update() {

		switch (state) {

		case STATE_MOVE:
			updateMove();
			break;
		case STATE_TRACK_MC_MOVE:

			break;
		case STATE_ATTACK_MC:

			break;
		case STATE_EXPLOSION:
			if (publicSprite.hasAnimationFinished(_currentAnimation, _currentFrame, _waitDelay))
				state = -1;
			break;
		default:
			break;
		}
		if (checkCollision(this, StateGameplay.tankMC)) {
			attack(StateGameplay.tankMC);
		}
	}

	public static Bullet creaBullet(Actor actor, int x, int y, boolean _attackMC, boolean _attackEnemy) {
		Bullet bullet = null;
		bullet = new Bullet(actor.m_x, actor.m_y, Bullet.BULLET_NORMAL_WIDHT, Bullet.BULLET_NORMAL_HEIGHT, " ", " ");
		bullet.attackMC = _attackMC;
		bullet.attackEnemy = _attackEnemy;
		if (Bullet.publicSprite == null)
			Bullet.publicSprite = new Sprite("sprite/actor/bullet.sprt");
		bullet.angleRadian = GameLib.getRadians(0, 0, actor.m_x + GameLayer.screenOffsetX - x, actor.m_y + GameLayer.screenOffsetY - y);

		bullet.angleDegree = GameLib.getDegrees(bullet.angleRadian);
		bullet.publicSprite.setAnim(bullet, 0, true, false);
		bullet.state = Bullet.STATE_MOVE;
		return bullet;
	}

	public void attack(Actor actor) {
		// TODO Auto-generated method stub

	}
}
